package com.gexin.service.notice;

import com.alibaba.fastjson.JSONObject;
import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.StringUtil;
import com.gexin.common.xapp.ContentFilteringKit;
import com.gexin.model.CommentZan;
import com.gexin.model.notice.NoticeNews;
import com.gexin.model.notice.NoticeNewsComment;
import com.gexin.model.notice.NoticeNewsCommentReply;
import com.gexin.model.notice.NoticeZan;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

public class NoticeNewsService {
	public static NoticeNewsService dao = new NoticeNewsService();

	/**
	 * 获取首页咨询
	 */
	public Page<NoticeNews> findIndexList() {
		NoticeNews news =new NoticeNews();
		news.setState(0);
		return	NoticeNews.dao.query(1,3,news);
	}

	/**
	 * 资讯评论
	 * @param noticeId 资讯id
	 * @param content 评论内容
	 * @param commentId 评论
	 * @param authUser
	 */
	public ApiResponse commentNotice(Integer noticeId , String content , Integer commentId , WeixinUser authUser) {
		NoticeNews noticeNews = NoticeNews.dao.findById(noticeId);
		if(noticeNews==null){
			return new ApiResponse(false,"资讯不存在!");
		}
		ApiResponse apiResponse =new ApiResponse();
		JSONObject result= JSONObject.parseObject(ContentFilteringKit.msgSecCheck(content));
		if (result.getString("errcode").equals("0")) {
			if(commentId==null){
				//一级评论
				NoticeNewsComment noticeNewsComment = new NoticeNewsComment();
				noticeNewsComment.setContent(content);
				noticeNewsComment.setCreateTime(new Date());
				noticeNewsComment.setNewsId(noticeId);
				noticeNewsComment.setState(0);
				noticeNewsComment.setUserId(authUser.getId());
				boolean save = noticeNewsComment.save();
				if(save){
					noticeNews.setCommentCount(noticeNews.getCommentCount()+1);
					noticeNews.update();
				}
				apiResponse = new ApiResponse(save,save?"评论成功!":"评论失败");
			}else{
				NoticeNewsCommentReply noticeNewsCommentReply = new NoticeNewsCommentReply();
				noticeNewsCommentReply.setCommentId(commentId);
				noticeNewsCommentReply.setContent(content);
				noticeNewsCommentReply.setCreateTime(new Date());
				noticeNewsCommentReply.setUserId(authUser.getId());
				boolean save = noticeNewsCommentReply.save();
				if(save){
					noticeNews.setCommentCount(noticeNews.getCommentCount()+1);
					noticeNews.update();
				}
				apiResponse = new ApiResponse(save,save?"回复成功!":"回复失败");
			}
			return apiResponse;
		}else if (result.getString("errcode").equals("87014")){
			apiResponse.setState(false);
			apiResponse.setMsg("回复失败,有敏感内容！");
			return  apiResponse;
		}else {
			apiResponse.setState(false);
			apiResponse.setMsg(result.getString("errmsg"));
			return apiResponse;
		}
	}

	public NoticeNews findInfo(Integer noticeId , WeixinUser authUser) {
		NoticeNews noticeNews = NoticeNews.dao.findById(noticeId);
		if(noticeNews!=null&&authUser!=null){
			NoticeZan noticeZan = NoticeZan.dao.findByUserIdAndNoticeId(authUser.getId() , noticeNews.getId());
			noticeNews.put("is_zan" , noticeZan != null);
		}else{
			noticeNews.put("is_zan" , false);
		}
		List<NoticeNewsComment> noticeNewsComments = NoticeNewsComment.dao.findByNoticeId(noticeId);
		for (NoticeNewsComment noticeNewsComment : noticeNewsComments) {
			if(authUser!=null) {
				CommentZan commentZan = CommentZan.dao.findByUserAndCommentId(authUser.getId() , noticeNewsComment.getId().toString());
				noticeNewsComment.put("is_zan" , commentZan != null);
			}else{
				noticeNewsComment.put("is_zan" , false);
			}
			List<NoticeNewsCommentReply> commentId = NoticeNewsCommentReply.dao.findCommentId(noticeNewsComment.getId());
			noticeNewsComment.put("replies",commentId);
		}
		noticeNews.put("comment",noticeNewsComments);
		return noticeNews;
	}

	/***
	 * 点击将未读变已读
	 * @param replyId
	 */
	public ApiResponse commentRead(String replyId) {
		if(StringUtil.isBlank(replyId)){
			return  new ApiResponse(false,"回复id不能为null!");
		}
		NoticeNewsCommentReply commentReply = NoticeNewsCommentReply.dao.findById(replyId);
		commentReply.setIsRead(1);
		boolean update = commentReply.update();
		return new ApiResponse(update,update?"更新成功!":"更新失败!");
	}
}
